package com.ll.controller;

import com.ll.domain.Account;
import com.ll.domain.Account2;
import com.ll.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 2020/9/12 - 15:07
 * 请求参数绑定
 */
@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     * 请求参数绑定入门
     * @return
     */
    @RequestMapping("/testParam")
    public String testParam(String username, String password){
        System.out.println("执行了 ParamController => testParam");
        System.out.println(username);
        System.out.println(password);
        return "success";
    }

    /**
     * 请求参数绑定,把数据封装到JavaBean中
     * @param account
     * @return
     */
    @RequestMapping("/saveAccount")
    public String saveAccount(Account account){
        System.out.println("执行了 ParamController => saveAccount");
        System.out.println(account);
        return "success";
    }

    /**
     * 数据封装在Account2类中,类中存在list和map集合
     * @param account2
     * @return
     */
    @RequestMapping("/saveAccount2")
    public String saveAccount2(Account2 account2){
        System.out.println("执行了 ParamController => saveAccount2");
        System.out.println(account2);
        return "success";
    }

    /**
     * 自定义类型转换器
     * @param user
     * @return
     */
    //当注解只有一个属性时,value可以省略
    @RequestMapping(value = "/saveUser")
    public String saveUser(User user){
        System.out.println("执行了 ParamController => saveUser");
        System.out.println(user);
        return "success";
    }

    /**
     * 原生的Servlet API
     * @return
     */
    @RequestMapping("/testServlet")
    public String testServlet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了 ParamController => testServlet");
        System.out.println(request);

        HttpSession session = request.getSession();
        System.out.println(session);

        ServletContext servletContext = session.getServletContext();
        System.out.println(servletContext);

        System.out.println(response);
        return "success";
    }


}
